package com.example.demo_mysql_mybatisplus.util.easyexcel.imports;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.example.demo_mysql_mybatisplus.util.easyexcel.imports.customized.DynamicImportListener;
import com.example.demo_mysql_mybatisplus.util.easyexcel.imports.dtos.ImportDataDTO;
import lombok.SneakyThrows;

import java.io.FileInputStream;

public class ImportDemo {

    public static void main(String[] args){
        new ImportDemo().exportUseAnnotation();
    }

    @SneakyThrows
    public void exportUseAnnotation( ){
        // 配置参数
        String file = "C:\\Users\\Administrator\\Desktop\\"+"useAnnotation_20231101_011445.xlsx";
        // 有个很重要的点 DataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去

        // 导入指定的sheet
        ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(new FileInputStream(file)).useDefaultListener(false).autoCloseStream(true) ;
        DynamicImportListener listener = new DynamicImportListener(ImportDataDTO.class) ;
        excelReaderBuilder.sheet(0).registerReadListener( listener ).doRead();
        System.out.println( listener.getDataList() );

        // 导入所有sheet
        ExcelReaderBuilder excelReaderBuilder2 = EasyExcel.read(new FileInputStream(file)).useDefaultListener(false).autoCloseStream(true) ;
        DynamicImportListener listenerAll = new DynamicImportListener(ImportDataDTO.class) ;
        excelReaderBuilder2.registerReadListener( listenerAll ).doReadAll();
        System.out.println( listenerAll.getDataList() );
    }



}